package com.bungarus.busi;

import com.bungarus.codec.protobuf.MessageProtobuf;
import com.bungarus.model.MessageType;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

/**
 * Created by tang on 2018/11/16.
 */
public class NodeAsClientMessageHandler extends ChannelInboundHandlerAdapter {
    Logger logger = Logger.getLogger(NodeAsClientMessageHandler.class);
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(msg instanceof MessageProtobuf.Message){
            MessageProtobuf.Message message = (MessageProtobuf.Message)msg;
            if(MessageType.ACK == message.getHeader().getType()){
                Discharger.discharge(message.getHeader().getId());
            }
            else if(MessageType.HEARTBEAT_PONG == message.getHeader().getType()) {
                logger.info(message.getHeader());
            }
        }
        else {
            super.channelRead(ctx, msg);
        }
    }
}
